package milktea.servlet.home;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import milktea.entity.CATEGORY;
import milktea.entity.PRODUCT;
import milktea.service.CATEGORYService;
import milktea.service.PRODUCTService;

/**
 * Servlet implementation class SelectProductList
 */
@WebServlet("/selectproductlist")
public class SelectProductList extends HttpServlet {


    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        ArrayList<CATEGORY> flist = CATEGORYService.selectCat("father");
        request.setAttribute("flist", flist);

        ArrayList<CATEGORY> clist = CATEGORYService.selectCat("child");
        request.setAttribute("clist", clist);


        String fid = request.getParameter("fid");
        String cid = request.getParameter("cid");

        int id = 0;

        ArrayList<PRODUCT> list = null;

        if (fid != null) {
            id = Integer.parseInt(fid);
            list = PRODUCTService.selectByAllFid(id);

        }
        if (cid != null) {
            id = Integer.parseInt(cid);
            list = PRODUCTService.selectByAllCid(id);
        }


        request.setAttribute("title", CATEGORYService.selectById(id).getCATE_NAME());

        request.setAttribute("list", list);
        request.getRequestDispatcher("productlist.jsp").forward(request, response);


    }


}
